
package com.crowdfunding.circle.data.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;
import com.crowdfunding.circle.data.entity.DataPlatformVO;
import com.crowdfunding.circle.data.entity.PlatformTradeVO;
import com.crowdfunding.circle.data.param.PlatformTradeParam;
import com.crowdfunding.circle.data.service.DataPlatformService;
import com.crowdfunding.circle.data.service.PlatformTradeService;

/**
 * 平台交易信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class PlatformTradeController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PlatformTradeController.class);

    @Autowired
    private PlatformTradeService platformTradeService;
    
    @Autowired
    private DataPlatformService dataPlatformService;
    
    private final static String LIST_PAGE = "/data/platform_trade_list_ajax";

    private final static String ADMIN_LIST_PAGE = "/admin/platform_trade_manage";
    
    private final static String ACCOUNT_LIST_PAGE = "/account/my_platform_trade";

    private final static String UPDATE_PAGE = "/data/platform_trade_edit";
    
    @RequestMapping(value = "/account/platformTrade/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");

        DataPlatformVO dataPlatformVO = dataPlatformService.findByUserId(seUser.getUserId());
        resultMap.put("dataPlatformVO", dataPlatformVO);

        PlatformTradeVO platformTradeVO = new PlatformTradeVO();
        if (dataPlatformVO != null && StringUtils.isNotBlank(dataPlatformVO.getPlatformId())) {
            platformTradeVO.setPlatformId(dataPlatformVO.getPlatformId());
        }

        resultMap.put("platformTradeVO", platformTradeVO);

        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/account/platformTrade/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PlatformTradeVO platformTradeVO = platformTradeService.findById(id);
        resultMap.put("platformTradeVO", platformTradeVO);

        DataPlatformVO dataPlatformVO = dataPlatformService.findById(platformTradeVO.getPlatformId());
        resultMap.put("dataPlatformVO", dataPlatformVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/account/platformTrade/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(PlatformTradeVO platformTradeVO, String platformName) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            Integer affectCount = 0;
            if (platformTradeVO != null && StringUtils.isNotBlank(platformTradeVO.getPlatformTradeId())) {
                affectCount = platformTradeService.update(platformTradeVO);
            } else if (platformTradeVO != null) {

                affectCount = platformTradeService.insert(platformTradeVO, platformName, seUser.getUserId());
            }

            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改平台交易信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改平台交易信息失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改平台交易信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改平台交易信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/account/platformTrade/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String accountDelete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = platformTradeService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除平台交易信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除平台交易信息失败");
            }
        } catch (Exception e) {
            logger.error("删除平台交易信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除平台交易信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/account/platformTrade/list")
    public ModelAndView queryAccountList(PlatformTradeParam platformTradeParam) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");

        Map<String, Object> resultMap = new HashMap<String, Object>();

        DataPlatformVO dataPlatformVO = dataPlatformService.findByUserId(seUser.getUserId());
        resultMap.put("dataPlatformVO", dataPlatformVO);

        if (dataPlatformVO != null && StringUtils.isNotBlank(dataPlatformVO.getPlatformId())) {
            platformTradeParam.setPlatformId(dataPlatformVO.getPlatformId());
            List<PlatformTradeVO> dataList = platformTradeService.queryByPage(platformTradeParam);
            resultMap.put("dataList", dataList);
        } else {
            resultMap.put("dataList", new ArrayList<PlatformTradeVO>());
        }
        resultMap.put("paramMap", platformTradeParam);

        return new ModelAndView(ACCOUNT_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/platformTrade/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String adminDelete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = platformTradeService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除平台交易信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除平台交易信息失败");
            }
        } catch (Exception e) {
            logger.error("删除平台交易信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除平台交易信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/platformTrade/audit", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String audit(String platformTradeId, Integer auditStatus) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            PlatformTradeVO platformTradeVO = new PlatformTradeVO();

            platformTradeVO.setPlatformTradeId(platformTradeId);
            platformTradeVO.setAuditStatus(auditStatus);

            Integer affectCount = platformTradeService.update(platformTradeVO);
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "审核数据_平台信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "审核数据_平台信息失败");
            }
        } catch (Exception e) {
            logger.error("审核数据_平台信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "审核数据_平台信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/platformTrade/list")
    public ModelAndView queryAdminList(PlatformTradeParam platformTradeParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<PlatformTradeVO> dataList = platformTradeService.queryByPage(platformTradeParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", platformTradeParam);

        DataPlatformVO dataPlatformVO = dataPlatformService.findById(platformTradeParam.getPlatformId());
        resultMap.put("dataPlatformVO", dataPlatformVO);

        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/uncheck/platformTrade/list")
    public ModelAndView queryList(PlatformTradeParam platformTradeParam) {
        
        Map<String, Object> resultMap = new HashMap<String, Object>();

        if (platformTradeParam != null && StringUtils.isNotBlank(platformTradeParam.getPlatformId())) {
            platformTradeParam.setAuditStatus(2);
            List<PlatformTradeVO> dataList = platformTradeService.queryByPage(platformTradeParam);
            resultMap.put("dataList", dataList);
        } else {
            resultMap.put("dataList", new ArrayList<PlatformTradeVO>());
        }
        resultMap.put("paramMap", platformTradeParam);

        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
}
